package vn.fpt.fsoft.dao;

import java.util.*;

import org.hibernate.*;
import org.hibernate.criterion.*;

import vn.fpt.fsoft.entity.TrustDistrict;
import vn.fpt.fsoft.entity.TrustRegion;
import vn.fpt.fsoft.util.HibernateUtil;

/*
 * Class TrustDistrictDAO
 * 
 * Version 1.0
 * 
 * 22-8-2014
 * 
 * Modification Logs:
 * DATE	    				AUTHOR 	    				DESCRIPTION
 * -------------------------------------------------------------------------------
 * 22-8-2014                BangNguyen        Create TrustDistrictDAO
 * 
 * */
public class TrustDistrictDAO {
	
	/**
	 * Get trust districts base on region ID.
	 * @param regionID
	 * @return
	 */
	@SuppressWarnings("unchecked")
    public static List<TrustDistrict> getTrustDistrictsByRegion(Integer regionID){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx =null;
		List<TrustDistrict> ds = null;
		try{
			tx = session.beginTransaction();
			TrustRegion tr = new TrustRegionDAO().getTrustRegionByKey(regionID);
			Criteria cri = session.createCriteria(TrustDistrict.class);
			if (tr != null) {
				cri.add(Restrictions.eq("trustRegion", tr));
				ds= cri.list();
			}
			tx.commit();
		}catch(HibernateException ex){
			if(tx!=null){
				tx.rollback();
			}
		}finally{
			HibernateUtil.close();
		}
		return ds;
	}

	/**
	 * Get trust disttrict base on ID.
	 * @param id
	 * @return
	 */
	public static TrustDistrict getDistrictById(Integer id){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx =null;
		TrustDistrict td=null;
		try{
			tx = session.beginTransaction();
			td = (TrustDistrict) session.get(TrustDistrict.class,id);
			tx.commit();
			HibernateUtil.close();
		}catch(HibernateException ex){
			if (tx != null)
				tx.rollback();
			HibernateUtil.close();
		}
		return td;
	}
	
	/**
	 * Amend trust district.
	 * @param id
	 * @param name
	 * @param description
	 * @return
	 */
	public static boolean amendTrustDistrict(Integer id, String name,
			String description) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			TrustDistrict td = (TrustDistrict) session.get(TrustDistrict.class,id);
			td.setName(name);
			td.setDescription(description);
			tx.commit();
			HibernateUtil.close();
			return true;
		} catch (HibernateException ex) {
			if (tx != null)
				tx.rollback();
			HibernateUtil.close();
			return false;
		}
	}
	
	public static boolean amendTrustDistrict(TrustDistrict trustDistrict) {
	    Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.update(trustDistrict);
            tx.commit();
            HibernateUtil.close();
            return true;
        } catch (HibernateException ex) {
            if (tx != null)
                tx.rollback();
            HibernateUtil.close();
            return false;
        }
	}

	/**
	 * Add new trust district.
	 * @param district
	 * @return
	 */
	public static boolean addTrustDistrict(TrustDistrict district) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.getTransaction();
		try {
			tx.begin();
			session.save(district);
			tx.commit();
			HibernateUtil.close();
			return true;
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
			}
			HibernateUtil.close();
			return false;
		}
	}
}
